package com.ort.ezcalendar;

import java.util.Calendar;
import java.util.Date;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ListView;
import android.widget.TimePicker;

import com.markupartist.android.widget.ActionBar;

public class AlarmListActivity extends Activity {

	private ListView list;
	private Calendar date;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list_alarms);

		ActionBar actionBar = (ActionBar) findViewById(R.id.actionbar);
		actionBar.setHomeLogo(R.drawable.logo_blanco);

		list = (ListView) findViewById(R.id.listAlarms);

		date = Calendar.getInstance();

		initValuesToList();

	}

	private void initValuesToList() {
		
		String alarms[] =  new String[Sistema.getInstance().getAlarmList().size()];
		
		for (int i = 0; i < alarms.length; i++) {
			Date d = Sistema.getInstance().getAlarmList().get(i);
			alarms[i] = "Fecha: " + d.getDate() + "/" + d.getMonth() + "/" + d.getYear() + "     Hora: " + d.getHours() + ":" + d.getMinutes();
		}
		
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, alarms);

		// Assign adapter to ListView
		list.setAdapter(adapter);
	}

	public void onClickBtnDateAlarm(View v) {
		final Dialog dialog = crearDialogo(R.layout.dialogo_fecha,
				"Ingrese la Alarma");
		final TimePicker pickerHora = (TimePicker) dialog
				.findViewById(R.id.hour);
		pickerHora.setIs24HourView(true);
		Button buttonAceptar = (Button) dialog
				.findViewById(R.id.btnConfirmarFecha);
		buttonAceptar.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				DatePicker pickerDate = (DatePicker) dialog
						.findViewById(R.id.date);
				int year = pickerDate.getYear();
				int month = pickerDate.getMonth();
				int day = pickerDate.getDayOfMonth();
				date.set(Calendar.DAY_OF_MONTH, day);
				date.set(Calendar.MONTH, month);
				date.set(Calendar.YEAR, year);

				int hour = pickerHora.getCurrentHour();
				int minutes = pickerHora.getCurrentMinute();
				date.set(Calendar.HOUR_OF_DAY, hour);
				date.set(Calendar.MINUTE, minutes);
				Sistema.getInstance().addAlarm(date.getTime());
				
				initValuesToList();

				dialog.dismiss();
			}
		});
	}

	private Dialog crearDialogo(int layout, String titulo) {
		final Dialog dialog = new Dialog(this);
		dialog.setContentView(layout);
		dialog.setTitle(titulo);
		dialog.setCancelable(true);
		dialog.show();
		return dialog;
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}
}
